package com.kclm.xsap.web.controller;

import com.kclm.xsap.entity.GlobalReservationSetEntity;
import com.kclm.xsap.service.GlobalSetService;
import com.kclm.xsap.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 团课预约设置控制器
 */
@Slf4j
@Controller
@RequestMapping("/globalSet")
public class GlobalSetController {

    @Autowired
    private GlobalSetService globalSetService;


    /**
     * 跳转到团课预约设置页面
     */
    @GetMapping("/x_course_reservation.do")
    public String courseReservation(Model model) {
        List<GlobalReservationSetEntity> globalReservationSetEntityList = globalSetService.list(null);
        GlobalReservationSetEntity globalReservationSetEntity = globalReservationSetEntityList.get(0);
        model.addAttribute("GLOBAL_SET", globalReservationSetEntity);
        model.addAttribute("endTimeStr", globalReservationSetEntity.getEndTime().format(DateTimeFormatter.ofPattern("HH:mm")));
        model.addAttribute("cancelTimeStr", globalReservationSetEntity.getCancelTime().format(DateTimeFormatter.ofPattern("HH:mm")));
        return "course/x_course_reservation";
    }

    /**
     * 修改团课预约设置
     */
    @PostMapping("/globalSetUpdate.do")
    @ResponseBody
    public R globalSetUpdate(GlobalReservationSetEntity globalReservationSetEntity) {
        boolean isUpdate=globalSetService.isUpdate(globalReservationSetEntity);
        if (isUpdate){
            return R.ok();
        }else {
            return R.error("请重新设置");
        }
    }
}
